<?php
namespace Home\Controller;
use Common\Controller\BaseController;
use Common\Model\RoomModel;
use Common\Model\StaffModel;

class SystemController extends BaseController{

	public function _initialize(){
		parent::_initialize();
		self::$_action_block = 7;
		$this->assign('block',self::$_action_block);
	}

	/**
	 * 获取管理员列表
	 */
	public function index(){
		$this->action = 1;
		$name = I('get.c');
		$where = !empty($name) ? " locate(user_name,'{$name}') or staff_id in(".intval($name).")": ' 1';
		$adminuserModel = new StaffModel();
		$list = $adminuserModel->getStaffList($where);
		$this->assign('type',$this->action);
		$this->assign('list',$list);
		$this->display('index');

	}

	/**
	 * 添加管理员,显示添加页面
	 */
	public function add(){
		$this->action = 2;
		$stafModel = new StaffModel();
		$tmgr = $stafModel->getManagerList();
		$this->assign('tmgrs',$tmgr);
		$this->assign('type',$this->action);
		$this->display('add');

	}
	public function allocate($staff_id){
		$this->action = 1;
		$this->assign('type',$this->action);
		$staffModel = new StaffModel();
		if(IS_GET){
			$staff_id = I('get.staff_id');
			$staff = $staffModel->where('staff_id='.intval($staff_id))->select();
			if(empty($staff)){
				$this->error('用户不存在');
			}
			$staff = $staff[0];
			$staff['rooms'] = explode("|",$staff['rooms']);
			$roomModel = new RoomModel();
			$rooms = $roomModel->getRoom();
			$this->assign('rooms',$rooms);
			$this->assign('staff',$staff);
			$this->assign('staff_id',$staff_id);
			$this->display('allocate');
			exit;
		}
		$staff_id = I('post.staff_id');
		$rooms    = I('post.room');
		if(is_array($rooms) && !empty($rooms)){
			$rooms = implode('|',$rooms);
		}
		$save = array('staff_id'=>$staff_id,'rooms'=>$rooms);

		$res = $staffModel->save($save);
		if(false !== $res){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}

	}
	/**
	 * 修改管理员信息，显示修改页面
	 */
	public function edit(){

		$id = I('get.id');

		$id = intval($id);
		$staffModel = new StaffModel();
		$info = $staffModel->getStaffById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$this->assign('info',$info);
		$this->display('edit');
	}

	/**
	 * 删除管理员
	 */
	public function delete(){

		$id = $this->_get('id');
		$id = intval($id);
		$info  = D('AdminUsers')->getInfoById($id);
		if(empty($info)){
			$this->error('数据不存在');
		}
		$res = D('AdminUsers')->where('id='.$id)->delete();
		if(false !==$res){
			$this->success('删除成功',U('/User/getAdmin/'));
		}else{
			$this->error('删除失败');
		}
	}
	/**
	 * 保存添加修改数据
	 */
	public function save(){
		$post = I('post.');
		$staffModel = new StaffModel();
		$res  = $staffModel->addStaff($post);
		if($res['status']){
			$this->success($res['info']);
		}else{
			$this->error($res['info']);
		}
	}

	/**
	 * 修改密码
	 */
	public function pass(){
		$data = $this->_post();

		if(empty($data)){
			$this->display('pass');
			exit;
		}

		$res  = D('Users')->changePass($data);

		if($res['status']){
			$this->success($res['info']);
		}else{
			$this->error($res['info']);
		}
	}
}
